-- TriggerDialogM
-- created by huanghr
-- 触发引导对话模块

module("TriggerDialogM", package.seeall);

local TriggerDialogInfo = {};
local exploreIds = {};

local rules = {};

-- 内部函数声明
local tryStartDialog;
local tryTempleGuide;

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    TriggerDialogInfo = {};

    -- 转换下trigger_dialog_CSV信息表
    local trigger_dialog_CSV = get_trigger_dialog_CSV();
    for i = 1, #trigger_dialog_CSV do
        local row = trigger_dialog_CSV[i];
        TriggerDialogInfo[row.id] = row;

        if row["trigger_rule"] == "sky_explore" then
            table.insert(exploreIds, row["event_id"]);
        end
    end
end

-- 模块初始化操作
function init()
    loadCsv();

    EventMgr.removeAll("TriggerDialogM");

    -- 注册浮岛信息更新的回调
    EventMgr.register("TriggerDialogM", event.SKY_CITY_EXPLORE, tryStartDialog);
    EventMgr.register("TriggerDialogM", event.FIND_TREASURE_TEMPLE, tryTempleGuide);
end

function query(id, path)
    local m = TriggerDialogInfo[id];

    if not m or not path then
        return m;
    end

    return m[path];
end

function getExploreIds()
    return exploreIds;
end

function getIdByEventId(eventId)
    if eventId == nil then
        return nil;
    end

    for k, value in pairs(TriggerDialogInfo) do
        if value["event_id"] == eventId then
            return k;
        end
    end

    return nil;
end

function getIdByEventType(eventType)
    if eventType == nil then
        return nil;
    end

    for k, value in pairs(TriggerDialogInfo) do
        if value["event_type"] == eventType then
            return k;
        end
    end

    return nil;
end

-- 尝试对话
tryStartDialog = function(args)
    -- 当前探索到的数据
    local data = args.data;
    local eventId = data.id;
    local eventType = SkyExploreM.query(eventId, "rule");
    local id;

    -- 优先根据事件ID查找
    local tempId = getIdByEventId(eventId);

    if tempId then
        id = tempId;
    else
        id = getIdByEventType(eventType);
    end

    tryTrigger(id);
end

-- 尝试触发神殿引导
tryTempleGuide = function(args)
    -- 当前探索到的数据
    local eventId =args.treasure_id;
    local eventType = args.rule;
    local id;

    -- 优先根据事件ID查找
    local tempId = getIdByEventId(eventId);

    if tempId then
        id = tempId;
        cclog("当前的ID：" .. tempId);
    else
        id = getIdByEventType(eventType);
    end

    tryTrigger(id);
end

-- 尝试触发对话
function tryTrigger(id)
    if id == nil then
        return;
    end
    local state = BuffDataM.query("trigger_dialog_log", id) or 0;

    -- 未触发尝试触发对话
    if state == 0 then
        BuffDataM.set("trigger_dialog_log", id, 1);

        local talkTemplate = query(id, "talk_template");
        local args = {["talkTemplate"] = talkTemplate, ["id"] = id,};

        local dbase = query(id, "dbase");
        local bonus = dbase["bonus"];
        if type(bonus) == "table" then
            args.bonus = bonus;
        end

        -- 触发引导
        EventMgr.fire(event.TRIGGER_ONCE_GUIDE, args);

        finishOnceGuide(id);
    end
end

function finishOnceGuide(id)
    Operation.cmd_try_finish_once_guide(id);
end